﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public partial class formInputTemplateName : Form
    {
        public string Result
        {
            get
            {
                return tbInput.Text;
            }
        }
        public formInputTemplateName()
        {
            InitializeComponent();
        }

        public formInputTemplateName(string pInput)
            : this()
        {
            tbInput.Text = pInput;
        }

        private void tbInput_TextChanged(object sender, EventArgs e)
        {
            var _validated = Program.CurrentFile.ValidateTemplateName(tbInput.Text);
            switch (_validated)
            {
                case TemplateNameValidationError.None:
                    {
                        lbError.ForeColor = Color.DarkGreen;
                        lbError.Text = Resources.TEMPLATEName_Valid;
                        lbAlternative.Text = String.Empty;
                        btnUseIt.Enabled = false;
                        btnUseIt.Visible = false;
                        btnOK.Enabled = true;
                        break;
                    }
                case TemplateNameValidationError.Invalid:
                    {
                        lbError.ForeColor = Color.Red;
                        lbError.Text = (tbInput.Text.Length > 0) ?
                            Resources.TEMPLATEName_Rules :
                            String.Empty;
                        lbAlternative.Text = String.Empty;
                        btnUseIt.Enabled = false;
                        btnUseIt.Visible = false;
                        btnOK.Enabled = false;
                        break;
                    }
                case TemplateNameValidationError.Taken:
                    {
                        lbError.ForeColor = Color.Red;
                        lbError.Text = Resources.TEMPLATEName_Taken;

                        var _alternative = Program.CurrentFile.GetFirstAvailableAlternative(tbInput.Text);

                        if (_alternative != String.Empty)
                        {
                            btnUseIt.Enabled = true;
                            btnUseIt.Visible = true;
                            btnUseIt.Tag = _alternative;
                            lbAlternative.Text = Resources.TEMPLATEName_SuggestAlternative.Replace("#",
                                _alternative);
                        }
                        
                        btnOK.Enabled = false;
                        break;
                    }
                default:
                    break;
            }
            
        }

        private void btnUseIt_Click(object sender, EventArgs e)
        {
            tbInput.Text = btnUseIt.Tag as string;
        }

        private void formInputTemplateName_Load(object sender, EventArgs e)
        {
            tbInput_TextChanged(null, null);
        }
    }
}
